/* === LICENSE INFO ===
 * This source file is subject to the GPLv3 license that is bundled with this
 * package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl.txt
 * === end of LICENSE INFO === */

#include "TestCaseDialog.h"
#include "ui_TestCaseDialog.h"
#include "MsgCommDialog.h"

TestCaseDialog::TestCaseDialog(QWidget *parent)
    : QDialog(parent), ui(new Ui::TestCaseDialog)
{
    ui->setupUi(this);
    ResetDisplay();
}

TestCaseDialog::~TestCaseDialog()
{
    delete ui;
}

void TestCaseDialog::ResetDisplay()
{
    this->setWindowTitle(tr("Test Case - New Test Case [New Test Plan]"));

    ui->txtlineTestCaseName->setText(tr("New Test Case"));
    ui->txtlineTesCaseVersion->setText(tr("(Non-versioned)"));
    ui->txtplainTestCaseRemarks->setPlainText(tr(""));
    ui->txtlineConnectionIP->setText(tr("127.0.0.1"));
    ui->txtlineConnectionPort->setText(tr("10000"));
    ui->txtlineConnectionTimeout->setText(tr("5000"));

    ui->btnlinkTestCaseVersion->setEnabled(false);
    ui->btnDeleteMsgComm->setEnabled(false);
    ui->btnOpenMsgComm->setEnabled(false);

    // Combo boxes for Protocol and Role
    // TODO (2009-05-13):
    //   Will need to refactor this code, probably to encapsulate
    //   each protocol and its supported roles as object for easier
    //   feature extension in future.
    ui->cmbTestCaseProtocol->clear();
    ui->cmbTestCaseProtocol->addItem(tr("TCP"));
    ui->cmbTestCaseProtocol->addItem(tr("UDP"));

    ui->cmbTestCaseRole->clear();
    ui->cmbTestCaseRole->addItem(tr("Client"));
    ui->cmbTestCaseRole->addItem(tr("Server"));

    // Reset the table of Message Communication List.
    // 3 colums: Msg to be Send, Msg to be Receive, Condition to Proceed.
    ui->tableMsgCommList->setRowCount(0);
    ui->tableMsgCommList->setColumnCount(3);

    ui->tableMsgCommList->setColumnWidth(0, 140);
    ui->tableMsgCommList->setColumnWidth(1, 140);
    ui->tableMsgCommList->setColumnWidth(2, 120);
}

// Slots
void TestCaseDialog::on_btnCancel_clicked()
{
    close();
}

void TestCaseDialog::on_btnOK_clicked()
{
    close();
}

void TestCaseDialog::on_btnAddMsgComm_clicked()
{
    MsgCommDialog dialog;
    dialog.exec();
}

void TestCaseDialog::on_cmbTestCaseProtocol_currentIndexChanged(int index)
{
    if (index == 0)
    {
        ui->cmbTestCaseRole->clear();
        ui->cmbTestCaseRole->addItem(tr("Client"));
        ui->cmbTestCaseRole->addItem(tr("Server"));
    }
    else
    {
        ui->cmbTestCaseRole->clear();
        ui->cmbTestCaseRole->addItem(tr("Sender"));
        ui->cmbTestCaseRole->addItem(tr("Receiver"));
    }
}

void TestCaseDialog::on_cmbTestCaseRole_currentIndexChanged(int index)
{
    QStringList headerLabel;
    if (index == 0)
    {
        headerLabel << "Msg to be Send" << "Msg to be Receive" << "Condition to Proceed";
        ui->tableMsgCommList->setHorizontalHeaderLabels(headerLabel);
        ui->grpboxProtocolConfig->setTitle(tr("Connecting to"));
    }
    else
    {
        headerLabel << "Msg to be Receive" << "Msg to be Send" << "Condition to Proceed";
        ui->tableMsgCommList->setHorizontalHeaderLabels(headerLabel);
        ui->grpboxProtocolConfig->setTitle(tr("Connection from"));
    }
}
